package com.ruoyi.writer.controller;

import java.io.File;
import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.domain.*;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.writer.domain.Chapter;
import com.ruoyi.writer.service.IChapterService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import org.springframework.web.multipart.MultipartFile;
import static org.jacoco.agent.rt.internal_43f5073.core.runtime.AgentOptions.OutputMode.file;

/**
 * 写作Controller
 * 
 * @author fst
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/writer")
public class ChapterController extends BaseController
{
    @Autowired
    private IChapterService chapterService;
    @Resource
    private NovelFeignService novelFeignService;
    @Resource
    private RemoteFileService remoteFileService;


    /**
     * 查询写作列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Chapter chapter)
    {
        startPage();
        List<Chapter> list = chapterService.selectChapterList(chapter);
        return getDataTable(list);
    }

    /**
     * 导出写作列表
     */

    @Log(title = "写作", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Chapter chapter)
    {
        List<Chapter> list = chapterService.selectChapterList(chapter);
        ExcelUtil<Chapter> util = new ExcelUtil<Chapter>(Chapter.class);
        util.exportExcel(response, list, "写作数据");
    }

    /**
     * 获取写作详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(chapterService.selectChapterById(id));
    }



    /**
     * 修改写作
     */

    @Log(title = "写作", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Chapter chapter)
    {
        return toAjax(chapterService.updateChapter(chapter));
    }

    /**
     * 删除写作
     */

    @Log(title = "写作", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(chapterService.deleteChapterByIds(ids));
    }

    /**
     * 新增写作
     */

    @Log(title = "写作", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Chapter chapter)
    {
        return toAjax(chapterService.insertChapter(chapter));
    }

    /**
     * 根据用户id，查询用户的作品
     */
    @GetMapping("/user")
    public AjaxResult getUserNovel(@RequestParam("userId") Long userId){
        List<NovelDTO> novelDTO = chapterService.getUserNovel(userId);
        return AjaxResult.success(novelDTO);
    }

    /**
     * 根据小说名称，查询小说的详情
     */
    @GetMapping("/novel")
    public AjaxResult getNovelDetail(@RequestParam("title") String novelName){
        AjaxResult novelDTO = novelFeignService.getNovelContent(novelName);
        return AjaxResult.success(novelDTO);
    }
    /**
     * 根据小说名称，查询小说的章节
     */
    @GetMapping("/chapter")
    public AjaxResult getNovelChapter(@RequestParam("title") String novelName){
        TableDataInfo novelDTO = novelFeignService.getNovelChapter(novelName);
        return AjaxResult.success(novelDTO);
    }

    /**
     * 根据小说名称，更改小说基本信息
     */
    @PutMapping("/updateNovel")
    public AjaxResult updateNovel(@RequestBody NovelVo novelVo){
        return AjaxResult.success(chapterService.updateNovel(novelVo));
    }
    /**
     * 上传小说封面
     */
    @PostMapping("/uploadCover")
    public R<SysFile> uploadCover(@RequestBody MultipartFile file) { // 正确：使用 @RequestParam
        R<SysFile> upload = remoteFileService.upload(file);
        return upload;
    }
    /**
     * 根据小说名称，插入小说内容
     */
    @PostMapping("/InsertNovel")
    public AjaxResult InsertNovel(@RequestBody ChapterDTO chapterDTO){
        return AjaxResult.success(chapterService.InsertNovel(chapterDTO));
    }

    /**
     * 根据章节号和小说名称，查询章节具体内容
     */
    @GetMapping("/getChapterContent")
    public AjaxResult getChapterContent(@RequestParam("title") String novelName,@RequestParam("orderNum") String orderNum){
        return novelFeignService.getContent(novelName,orderNum);
    }
    /**
     * 根据小说名称和章节号，更新章节内容
     */
    @PutMapping("/updateChapterContent")
    public AjaxResult updateChapter(@RequestBody ChapterDTO chapterDTO){
        return AjaxResult.success(chapterService.updateChapterContent(chapterDTO));
    }
    /**
     * 新增小说
     */
    @PostMapping("/addNovel")
    public AjaxResult addNovel(@RequestBody NewNovel newNovel) {
        return AjaxResult.success(chapterService.addNovel(newNovel));
    }
}
